package Demo11;

import Demo03.User;
import Demo03.UserDAOImpl;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import org.apache.struts2.ServletActionContext;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;


//          自定义拦截器 ,在对象之后，方法之前执行
public class Login extends ActionSupport implements ModelDriven<User> {

    private User user=new User();
    @Override
    public User getModel() {
        return user;
    }
    private List<User> list = new ArrayList<>();

    public List<User> getList() {
        return list;
    }

    public String toaddUser(){
        return "add";
    }

    public String login() throws Exception {
        String u=new UserDAOImpl().SelUser(user.getUsername());
        HttpServletRequest request =ServletActionContext.getRequest();
        if (u!=null&&u.equals(user.getPassword())){
           request.getSession().setAttribute("username",user.getUsername());
           list=new UserDAOImpl().selAll();
           return SUCCESS;
        }
        else{
            return "fail";
        }
    }
}
